package org.snare.alumni.modules.app;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.snare.alumni.modules.admin.card.dto.CardInfoQry;
import org.snare.alumni.modules.admin.student.entity.StudentEntity;
import org.snare.alumni.modules.admin.student.mapper.StudentMapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "校园卡")
@RequestMapping("/alumni/app/student")
@RestController
@RequiredArgsConstructor
public class StudentController {

    private final StudentMapper studentMapper;

    @GetMapping("/match")
    @ApiOperation(value = "根据证件号码查询", httpMethod = "GET")
    List<StudentEntity> match(CardInfoQry qry) {
//        String uid = JwtUtils.getUid();
        List<StudentEntity> list = new LambdaQueryChainWrapper<>(studentMapper)
                .eq(StudentEntity::getName, qry.getName())
//                .eq(CardEntity::getStatus, "0")
                .and(w -> w.isNull(StudentEntity::getUid).or().eq(StudentEntity::getUid, ""))
                .list();

//        List<CardDTO> cardDTOList = new ArrayList<>();
//        for (StudentEntity entity : list) {
//            CardDTO cardDTO = BeanUtils.copy(cardEntity, CardDTO.class);
//            if (StringUtils.isNotBlank(cardEntity.getImages())) {
//                cardDTO.setImgList(JSON.parseArray(cardEntity.getImages(), FileUploadDTO.class));
//            }
//            cardDTOList.add(cardDTO);
//        }
        return list;
    }

    @PostMapping("/upload")
    @ApiOperation(value = "同学录导入", httpMethod = "POST")
    void upload(@RequestParam("file") MultipartFile multipartFile) {
        Workbook workbook = null;
        InputStream inputStream = null;
        try {
            inputStream = multipartFile.getInputStream();
            workbook = new XSSFWorkbook(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            int rowCount = 0;
            ArrayList<StudentEntity> list = new ArrayList<>();
            for (Row row : sheet) {
                StudentEntity entity = new StudentEntity();
                // 跳过标题栏
                if (rowCount == 0) {
                    rowCount++;
                    continue;
                }
                for (Cell cell : row) {
                    if (cell.getColumnIndex() == 0) {
                        entity.setGraduationYear(cell.getStringCellValue().replace("届", ""));
                    } else if (cell.getColumnIndex() == 1) {
                        entity.setGraduationClass(cell.getStringCellValue());
                    } else if (cell.getColumnIndex() == 2) {
                        entity.setName(cell.getStringCellValue());
                    }
                }
                // 已经导入
                entity.setStatus("0");
                list.add(entity);
            }
            studentMapper.insert(list);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (workbook != null) {
                    workbook.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
